{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# max_flow_winston1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/max_flow_winston1.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/max_flow_winston1.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Max flow problem in Google CP Solver.\n",
    "\n",
    "  From Winston 'Operations Research', page 420f, 423f\n",
    "  Sunco Oil example.\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/max_flow_winston1.mzn\n",
    "  * Comet: http://hakank.org/comet/max_flow_winston1.co\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver('Max flow problem, Winston')\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 5\n",
    "  nodes = list(range(n))\n",
    "\n",
    "  # the arcs\n",
    "  # Note:\n",
    "  # This is 1-based to be compatible with other\n",
    "  # implementations.\n",
    "  arcs1 = [[1, 2], [1, 3], [2, 3], [2, 4], [3, 5], [4, 5], [5, 1]]\n",
    "\n",
    "  # convert arcs to 0-based\n",
    "  arcs = []\n",
    "  for (a_from, a_to) in arcs1:\n",
    "    a_from -= 1\n",
    "    a_to -= 1\n",
    "    arcs.append([a_from, a_to])\n",
    "\n",
    "  num_arcs = len(arcs)\n",
    "\n",
    "  # capacities\n",
    "  cap = [2, 3, 3, 4, 2, 1, 100]\n",
    "\n",
    "  # convert arcs to matrix\n",
    "  # for sanity checking below\n",
    "  mat = {}\n",
    "  for i in nodes:\n",
    "    for j in nodes:\n",
    "      c = 0\n",
    "      for k in range(num_arcs):\n",
    "        if arcs[k][0] == i and arcs[k][1] == j:\n",
    "          c = 1\n",
    "      mat[i, j] = c\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  flow = {}\n",
    "  for i in nodes:\n",
    "    for j in nodes:\n",
    "      flow[i, j] = solver.IntVar(0, 200, 'flow %i %i' % (i, j))\n",
    "\n",
    "  flow_flat = [flow[i, j] for i in nodes for j in nodes]\n",
    "\n",
    "  z = solver.IntVar(0, 10000, 'z')\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(z == flow[n - 1, 0])\n",
    "\n",
    "  # capacity of arcs\n",
    "  for i in range(num_arcs):\n",
    "    solver.Add(flow[arcs[i][0], arcs[i][1]] <= cap[i])\n",
    "\n",
    "  # inflows == outflows\n",
    "  for i in nodes:\n",
    "    s1 = solver.Sum([\n",
    "        flow[arcs[k][0], arcs[k][1]] for k in range(num_arcs) if arcs[k][1] == i\n",
    "    ])\n",
    "    s2 = solver.Sum([\n",
    "        flow[arcs[k][0], arcs[k][1]] for k in range(num_arcs) if arcs[k][0] == i\n",
    "    ])\n",
    "    solver.Add(s1 == s2)\n",
    "\n",
    "  # sanity: just arcs with connections can have a flow\n",
    "  for i in nodes:\n",
    "    for j in nodes:\n",
    "      if mat[i, j] == 0:\n",
    "        solver.Add(flow[i, j] == 0)\n",
    "\n",
    "  # objective: maximize z\n",
    "  objective = solver.Maximize(z, 1)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  db = solver.Phase(flow_flat, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db, [objective])\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print('z:', z.Value())\n",
    "    for i in nodes:\n",
    "      for j in nodes:\n",
    "        print(flow[i, j].Value(), end=' ')\n",
    "      print()\n",
    "    print()\n",
    "\n",
    "  print('num_solutions:', num_solutions)\n",
    "  print('failures:', solver.Failures())\n",
    "  print('branches:', solver.Branches())\n",
    "  print('WallTime:', solver.WallTime(), 'ms')\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
